﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace TVC_WEB.Global
{
    public partial class Order : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                if (Session["cart"] != null && Session["Username"] != null)
                {
                    List<DTO.CartInfo> lcart = Session["cart"] as List<DTO.CartInfo>;
                    BUS.ProductsControl ctr = new BUS.ProductsControl();
                    DataTable dt = ctr.LoadCart(lcart, ref sErr);
                    double total = Convert.ToDouble(dt.Compute("SUM(SubTotal)", ""));
                    
                    //liVAT.Text = (total * 0.1).ToString("N0");

                    string username = Session["Username"].ToString();

                    BUS.UserControl uctr = new BUS.UserControl();
                    DTO.UserInfo uinf = uctr.Get_Users_Email(username, ref sErr);
                    txtEmail.Text = uinf.Email;
                    txtShipName.Text = txtcompanyname.Text = uinf.CompanyName;
                    txtShipCity.Text = txttowncity.Text = uinf.City;
                    txtaddress1.Text = uinf.Address1;
                    txtShipPhone.Text = txttelephone.Text = uinf.Mobile;
                    txtShipAddress.Text = uinf.Address2;
                    ddlcountry.SelectedValue = ddlShipContry.SelectedValue = uinf.Country;
                    hfUserID.Value = uinf.ID.ToString();
                    BUS.PaymentControl payCtr = new BUS.PaymentControl();
                    DataTable dtXX = payCtr.GetAll(ref sErr);
                    HiddenField1.Value = total.ToString();
                    ddlPayment.DataSource = dtXX;
                    ddlPayment.DataTextField = "NameVN";
                    ddlPayment.DataValueField = "ID";
                    ddlPayment.DataBind();
                    ddlPayment.SelectedIndex = 0;
                    
                    ddlPayment_SelectedIndexChanged(null, null);
                    
                }
                else if (Session["Username"] == null)
                {
                    Response.Redirect("login.aspx");
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            DTO.OrderInfo oinf = new DTO.OrderInfo();
            oinf.UserID = Convert.ToInt32(hfUserID.Value);

            oinf.Bill_Address = txtaddress1.Text;
            oinf.Bill_City = txttowncity.Text;
            oinf.Bill_Name = txtcompanyname.Text;
            oinf.Bill_Phone = txttelephone.Text;
            oinf.DateTime = DateTime.Today;
            oinf.Notes = txtmessage.Text;
            oinf.Ship_Address = txtShipAddress.Text;
            oinf.Ship_City = txtShipCity.Text;
            oinf.Ship_Name = txtShipName.Text;
            oinf.Ship_Phone = txtShipPhone.Text;
            oinf.Ship_Country = ddlShipContry.SelectedValue;
            oinf.Bill_Country = ddlcountry.SelectedValue;
            oinf.IDStatus = 2;
            oinf.OrderProducts = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(DateTime.Now.ToString()))) + "(" + DateTime.Now.ToString() + ")";
            oinf.IDPayment = Convert.ToInt32(ddlPayment.SelectedValue);
            if (Session["cart"] != null)
            {
                BUS.OrderProductControl opctr = new BUS.OrderProductControl();
                BUS.OrderControl octr = new BUS.OrderControl();

                List<DTO.CartInfo> lcart = Session["cart"] as List<DTO.CartInfo>;
                BUS.ProductsControl proCtr = new BUS.ProductsControl();
                DataTable dt = proCtr.LoadCart(lcart, ref sErr);
                int orderID = octr.Add(oinf, ref sErr);
                foreach (DataRow x in dt.Rows)
                {
                    DTO.OrderProductInfo odInf = new DTO.OrderProductInfo();
                    odInf.OrderProducts = oinf.OrderProducts;
                    odInf.Price = Convert.ToDouble(x["NewPrice"]);
                    odInf.ProductID = Convert.ToInt64(x["ID"]);
                    odInf.Quantity = Convert.ToInt32(x["Qty"]);
                    opctr.Add(odInf, ref sErr);
                }
                Session["cart"] = null;
                Response.Redirect("OrderResult.aspx?orderid=" + orderID);
            }
        }

        protected void ddlPayment_SelectedIndexChanged(object sender, EventArgs e)
        {
            BUS.PaymentControl payCtr = new BUS.PaymentControl();
            DTO.PaymentInfo payInf = payCtr.Get(Convert.ToInt32(ddlPayment.SelectedValue), ref sErr);
            liVAT.Text = payInf.Value1.ToString("#,##0.##");
            liTotal.Text = (Convert.ToDouble(HiddenField1.Value) + payInf.Value1).ToString("N0");
        }
    }
}
